<template lang="pug">
.registration-template
  mt-dialog(
    v-model="model"
    cancel-text="取消"
    size="large"
    :title="tileName"
    v-bind:onCancel="onCancel"
    v-bind:onConfirm="onConfirm"
    v-bind:onClose="onClose"
    :fullscreen="true"
  )
    editor(ref="foEditor")
</template>

<script setup lang="ts">

import {
  useNotify, editor
} from "@scmt/common-components";
import { useRequest } from "@scmt/common-hooks";
import { useApi } from "@scmt/core-api";
// import { useUtils } from "@scmt/lib-utils";
// const $utils = useUtils();
const foEditor = ref<InstanceType<typeof editor>>();
//添加 Props属性
const props = defineProps({
  selectRow: Object,
  action: String,
});
//定义事件
const emit = defineEmits(['handleQuery']);

const $notify = useNotify();
const $api = useApi()
const { request, data } = useRequest({
  templateByEquipmentName: async (name: string) => {
    return await $api.bdm.template.queryTemplateByTemplateName(name);
  },
  //查询登记单下面关联的项目
  registrationProject: async (params: any) => {
    return await $api.pacs.project.queryPacsProjectListByRegistrationId(params);
  },
});

const jsonValue = ref("");
const modeValue = ref("");

const model = defineModel({
  default: false,
});
//弹框标题
const tileName = ref<null | String>("打印申请单");

//弹框点击确认事件
const onConfirm = () => {
  model.value = false;

};
//关闭弹框
const onCancel = () => {
  model.value = false;
};
//取消
const onClose = () => {
  model.value = false;
};

/**
 * 查找模板数据
 * @param templatejson 模板json
 * @param key 查询的key
 * @param resKey  需要的返回值
 */
const quaryData = (templatejson: any, key: any, resKey: any): boolean => {
  let res = false;
  if (templatejson && key) {


    if (templatejson.content && templatejson.content.length > 0) {
      if (templatejson.attrs && key == templatejson.attrs.name) {
        resKey.value = templatejson.content[0].text
        res = true;
        return res;
      }
      for (let index = 0; index < templatejson.content.length; index++) {
        const element = templatejson.content[index];
        let resFor = quaryData(element, key, resKey);
        if (resFor) {
          res = resFor;
          break;
        }
      }
    }
  }
  return res;
};
watch(
  model,
  () => {
    if (model.value) {
      foEditor.value?.destroy();
      nextTick(() => {
        if (props.selectRow) {
          let item = props.selectRow;
          modeValue.value = "readonly";
          request("registrationProject", {
            deviceId: item.deviceId,
            registrationId: item.id,
          });
          request("templateByEquipmentName", "pacs检查申请单").then(() => {
            let resData = data.templateByEquipmentName ?? {};
            let resItem = $api.bdm.template.$$createEntity(resData);
            if (resData && resItem.jsonTemplate) {
              //匹配数据
              let json = JSON.parse(resItem.jsonTemplate);
              jsonValue.value = JSON.stringify(json);
            }
            nextTick(() => {
              item.Ttable = data.registrationProject ?? [];
              if (item.Ttable && item.Ttable.length > 0) {
                item.Ttable.forEach((entity: any) => {
                  entity.officeName = resItem.officeName;
                })
              }
              foEditor.value?.initEditor(modeValue.value, jsonValue.value ?? JSON.parse(jsonValue.value), item);
            });
          });

        }
        else {
          $notify.error("配置有误，请联系管理员！！");
          model.value = false;
        }
      });
    }
  },
  {
    immediate: true,
  }
);
</script>

<style lang="less" scoped>
.registration-template {

  //去除表格边框
  // :deep(.fo-content-wrapper [data-fo-node=table]>table>tbody>tr>td) {
  //   border: 0px solid #ddd;
  // }

  :deep(.mt-dialog__modal) {
    height: 100vh !important;

  }
}
</style>